#pragma once
#include "AbstractIResonser.h"
#include "AbstractHTTPServer.h"
#include "AbstractIRequest.h"
#include "IOCPNetwork.h"
#include "HTTPContent.h"
#include "HTTPResponserHeader.h"

class HTTPResponser :
	public AbstractIResonser
{
public:
	HTTPResponser(AbstractHTTPServer * server, IOCPNetwork* iocp);
	~HTTPResponser();

	/* 访问属性 */
	 void * getConnectionId();
	 __int64 getTotalSentBytes();
	 __int64 getTotalRecvBytes();
	 int getServerCode();

	/* 返回响应头 */
	 std::string getHeader();

	/* 控制运行 */
	 int run(void *connId, void * clientSock, AbstractIRequest *request);
	 bool stop(int ec);
	 bool reset();

protected:

	// 发送回调方法
	static void IOCPCallbackSend(void* s, int flags, bool result, int transfered, byte* buf, size_t len, void* param);

	// 开始发送
	void onSend(size_t bytesTransfered, int flags);

	// 关闭响应
	void close(int code);

protected:

	AbstractHTTPServer * server;

	IOCPNetwork* iocp;

	// 请求头信息
	char * socketBuff;

	// 请求头信息长度
	size_t socketBuffLength;

	// 发送长度
	size_t byteSend;

	// 客户端连接
	void * connId;

	// 客户连接socket
	void * clientSocket;

	AbstractIRequest * request;

	// 请求内容对象
	HTTPContent * httpContent;

	// 服务端返回给客户端的状态码
	unsigned int serverCode;

	HTTPResponserHeader reponseHeader;

private:
	// 生成响应头
	bool makeResponserHeader(unsigned int code);

};

